package com.sdkx.app.datalayers;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;

import com.sdkx.app.R;
import com.sdkx.app.ViewModel.TrackPointManager;
import com.sdkx.app.model.TrackPoint;

import java.util.ArrayList;
import java.util.List;

import cn.creable.gridgis.display.Display;
import cn.creable.gridgis.display.LineSymbol;
import cn.creable.gridgis.geodatabase.FeatureClass;
import cn.creable.gridgis.geometry.IEnvelope;
import cn.creable.gridgis.geometry.Point;
import cn.creable.gridgis.util.Image;

public class TackPointsPanel extends MapDraw {
    private Paint paint;
    LineSymbol lineSymbol = new LineSymbol();
    FeatureClass featureClass;
    Image posimg;
    List<TrackPoint> list = new ArrayList<>();
    IEnvelope mEnvelope;

    public TackPointsPanel(Context context) {
        super(context);
        paint = new Paint();
        paint.setTextSize(25);
        paint.setColor(0xFFFF0000);
        // list = TrackPointManager.getAll();
        BitmapDrawable bmpDraw = (BitmapDrawable) appContext.getResources().getDrawable(R.drawable.gps);
        posimg = new Image(bmpDraw.getBitmap());
    }

    @Override
    public void drawOnMapCache(Canvas canvas) {
        //super.drawOnMapCache(canvas);
    }

    @Override
    protected void draw2(Canvas canvas, Display display) {
        IEnvelope envelope = mapControl.getExtent();
        list = TrackPointManager.getPointsByScreen(envelope);
        Canvas oldG = display.getCanvas();
        display.setCanvas(canvas);
        TrackPoint point = null;
        for (int index = 0; index < list.size(); index++) {
            point = list.get(index);
            display.DrawImagePoint(new Point(point.getLongitude(), point.getLatitude()), posimg);
        }
        display.setCanvas(oldG);
    }

    @Override
    public void draw(Canvas canvas) {
        Display display = (Display) mapControl.getDisplay();
        //super.draw(canvas);
        Canvas oldG = display.getCanvas();
        display.setCanvas(canvas);
        TrackPoint point = null;
        if(list != null) {
            for (int index = 0; index < list.size(); index++) {
                point = list.get(index);
                display.DrawImagePoint(new Point(point.getLongitude(), point.getLatitude()), posimg);
            }
        }
        display.setCanvas(oldG);
    }

    @Override
    public void upData() {
        IEnvelope envelope = mapControl.getExtent();
        if (mEnvelope == null) {
            mEnvelope = envelope;
            list = TrackPointManager.getPointsByScreen(envelope);
            mapControl.refresh();
        } else {
            boolean lowerleft = mEnvelope.getXMax() == envelope.getXMax();
            boolean lowerright = mEnvelope.getXMin() == envelope.getXMin();
            boolean upperleft = mEnvelope.getYMax() == envelope.getYMax();
            boolean upperRight = mEnvelope.getYMin() == envelope.getYMin();
            if (!(lowerleft && lowerright && upperleft && upperRight)) {
                list = TrackPointManager.getPointsByScreen(envelope);
                mapControl.refresh();
            }
            mEnvelope = envelope;
        }
    }
}
